﻿using System.Collections.Generic;
using System.Linq;
using AutoMapper;
using System.Web.Mvc;
using CNav.Business.Services;
using CNav.Common;
using CNav.Domain;
using CNav.Model;
using CNav.Support;
using WebGrease.Css.Extensions;

namespace CNav.Controllers
{
    [Authorize]
    [AdminAuthorize]
    public class ProductController : BaseController
    {
        public ActionResult Index()
        {
            ResponseData<List<Product>> responseData = Service.Get<IProductService>().GetProducts();
            responseData.ValidateAndThrow();

            var productModels = responseData.Data.Select(Mapper.Map<ProductModel>).ToList();
            return View(productModels);
        }

        public ActionResult Details(int id = 0)
        {
            var response = Service.Get<IProductService>().GetProduct(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }
            var model = Mapper.Map<ProductModel>(response.Data);

            return View(model);
        }

        public ActionResult Create()
        {
            var productModel = new ProductModel();
            return View(productModel);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(ProductModel model)
        {
            if (ModelState.IsValid)
            {
                    var product = Mapper.Map<Product>(model);

                    var response = Service.Get<IProductService>().AddProduct(product);
                    if (response.Data)
                        return RedirectToAction("Index");

                    response.Error.ForEach(e => ModelState.AddModelError("", e));
            }

            return View(model);
        }


        public ActionResult Edit(int id = 0)
        {
            var response = Service.Get<IProductService>().GetProduct(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }
            var model = Mapper.Map<ProductModel>(response.Data);

            return View(model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(ProductModel model)
        {
            if (ModelState.IsValid)
            {
                var product = Mapper.Map<Product>(model);
                ResponseData<Product> response = Service.Get<IProductService>().UpdateProduct(product);

                if (response.Error == null)
                    return RedirectToAction("Index");

                response.Error.ForEach(e => ModelState.AddModelError("", e));
            }

            return View(model);
        }

        public ActionResult Delete(int id)
        {
            var response = Service.Get<IProductService>().GetProduct(id);
            if (response.Data == null)
            {
                return View("NotFound");
            }
            var model = Mapper.Map<ProductModel>(response.Data);

            return View(model);
        }


        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            var currentUser = LoginUser;
            if (currentUser.IsBusinessAdmin)
            {
                ResponseData<bool> responseData = Service.Get<IProductService>().DeleteProduct(id);
                if (responseData.Data)
                    return RedirectToAction("Index");
            }
            ModelState.AddModelError("", "You are not authorise to delete.");
            return RedirectToAction("Index");
        }
    }
}